package com.zhuiyun.project.api.products.product.controller;

import com.zhuiyun.project.annotation.OptLog;
import com.zhuiyun.project.api.products.product.service.ProductService;
import com.zhuiyun.project.api.products.product.vo.ProductSortVo;
import com.zhuiyun.project.api.products.product.entity.Product;
import com.zhuiyun.project.common.controller.BaseController;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.UploadFileUtils;
import com.zhuiyun.project.utils.constants.CommonConstants;
import com.zhuiyun.project.utils.pageUtil.PageRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.HashMap;

/**
 * @ClassName ProductController
 * @Description TODO 商品管理控制层
 * @Author ypq
 * @Date 2023/10/25 19:29
 **/
@RestController
@Api(tags = "商品管理")
@RequestMapping("/product")
public class ProductController extends BaseController {

    @Autowired
    private ProductService productService;

    /**
     * @Author ypq
     * @Description 查询商品
     * @Date 2023/10/25 19:34
     * @Return CommonResult
     * @param pageRequest
     * @param productVo
     */
    @PreAuthorize("@ss.hasAnyPermi('products:product:list')")
    @ApiOperation(value = "分页查询所有商品")
    @OptLog(modular = "商品管理",type = CommonConstants.SELECT, describe = "分页查询所有商品")
    @GetMapping("/listProductByPages")
    public CommonResult listProductsByPages(PageRequest pageRequest, ProductSortVo productVo){
        //将收到的值传入 service进行查询，查询成功返回数据
        return CommonResult.ok(productService.listProductByPages(pageRequest, productVo));
    }

    /**
     * @Author ypq
     * @Description 新增商品
     * @Date 2023/10/26 14:24
     * @Return com.zhuiyun.project.common.response.CommonResult
     * @param productSortVo
     */
    @PreAuthorize("@ss.hasPermi('products:product:add')")
    @ApiOperation(value = "新增商品")
    @OptLog(modular = "商品管理", type =CommonConstants.ADD, describe = "新增商品")
    @PostMapping("/addProduct")
    public CommonResult addProduct(@RequestBody ProductSortVo productSortVo) throws CommonException {
        //设置创建时间及创建人
        productSortVo.setCreateBy(getLoginName());
        productSortVo.setCreateTime(new Date());
        //设置修改时间及修改人
        productSortVo.setUpdateBy(getLoginName());
        productSortVo.setUpdateTime(new Date());
        //将收到的值传入 service进行新增，成功返回结果
        return CommonResult.ok(productService.addProduct(productSortVo));
    }

    /**
     * @Author ypq
     * @Description 根据商品编号获取信息
     * @Date 2023/10/26 19:32
     * @Return com.zhuiyun.project.common.response.CommonResult
     * @param productId
     */
    @PreAuthorize("@ss.hasPermi('products:product:query')")
    @ApiOperation(value = "根据商品编号获取信息")
    @OptLog(modular = "商品管理",type = CommonConstants.SELECT, describe = "根据商品编号获取信息")
    @GetMapping("/{productId}")
    public CommonResult getProductById(@PathVariable(value = "productId", required = false)  Long productId){
        //将收到的值传入 service进行查询
        ProductSortVo productVo= productService.getProductById(productId);
        //查询成功返回数据
        HashMap<String, Object> map = new HashMap<>();
        map.put("productVo",productVo);
        return CommonResult.ok(map);
    }

    /**
     * @Author ypq
     * @Description 修改商品
     * @Date 2023/10/26 14:24
     * @Return com.zhuiyun.project.common.response.CommonResult
     * @param productSortVo
     */
    @PreAuthorize("@ss.hasPermi('products:product:edit')")
    @ApiOperation(value = "修改商品")
    @OptLog(modular = "商品管理", type =CommonConstants.UPDATE, describe = "修改商品")
    @PutMapping("/editProduct")
    public CommonResult editProduct(@RequestBody ProductSortVo productSortVo) throws CommonException {
        //设置修改时间及修改人
        productSortVo.setUpdateBy(getLoginName());
        productSortVo.setUpdateTime(new Date());
        //将收到的值传入 service进行修改，成功返回结果
        return CommonResult.ok(productService.editProduct(productSortVo));
    }

    /**
     * @Author ypq
     * @Description 下架商品 方法
     * @Date 2023/10/26 16:51
     * @Return com.zhuiyun.project.common.response.CommonResult
     * @param product
     */
    @PreAuthorize("@ss.hasPermi('products:product:down')")
    @ApiOperation(value = "商品下架")
    @OptLog(modular = "商品管理", type = CommonConstants.UPDATE, describe = "下架商品")
    @PutMapping("/downProduct")
    public CommonResult downProduct(@RequestBody Product product){
        //设置修改时间及修改人
        product.setUpdateBy(getLoginName());
        product.setUpdateTime(new Date());
        //将收到的值传入 service进行修改，成功返回结果
        return CommonResult.ok(productService.downProduct(product));
    }

    /**
     * @Author ypq
     * @Description 商品图片上传
     * @Date 2023/10/28 18:15
     * @Return com.zhuiyun.project.common.response.CommonResult
     * @param file
     */

    @ApiOperation(value = "商品图片上传")
    @PostMapping("/upload")
    public CommonResult upload(MultipartFile file){
        //获取图片id
        String imgId = UploadFileUtils.uploadFile(file);
        //返回给前端
        return CommonResult.ok(imgId);
    }

    /**
     * @Author ypq
     * @Description 根据分类id查询商品
     * @Date 2023/10/25 19:34
     * @Return CommonResult
     * @param productVo
     */
    @ApiOperation(value = "根据分类id查询商品")
    @GetMapping("/listProducts")
    public CommonResult listProducts(ProductSortVo productVo){
        //将收到的值传入 service进行查询，查询成功返回数据
        return CommonResult.ok(productService.listProducts(productVo));
    }
    
    /**
    * @Author ypq
    * @Description 搜索商品
    * @Date 2023/11/14 14:07
    * @Return CommonResult
    * @param productVo
    */
    @ApiOperation(value = "app端根据商品名称查询商品及分类")
    @GetMapping("/listProductByNames")
    public CommonResult listProductByNames(ProductSortVo productVo){
        //将收到的值传入 service进行查询，查询成功返回数据
        return CommonResult.ok(productService.listProductByNames(productVo));
    }
}
